
import 'package:favorcate/core/model/category_model.dart';
import 'package:favorcate/core/services/http_request.dart';
import 'package:favorcate/core/services/http_request_model.dart';
import 'package:favorcate/core/services/http_url.dart';
import 'package:favorcate/core/services/json_parse.dart';
import 'package:favorcate/ui/pages/home/home_category_item.dart';
import 'package:favorcate/ui/shared/size_fit.dart';
import 'package:flutter/material.dart';

/*
class JKHomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<JKCategoryModel>>(
      future: JKJsonParse.getCategoryData(),
      builder: (ctx, snapshot) {
        if(!snapshot.hasData) return Center(child: CircularProgressIndicator(),);
        if(snapshot.error != null ) return Center(child: Text('请求失败'),);

        print('-------------${snapshot.data}-------------');
        final _categories = snapshot.data;
        return GridView.builder(
            itemCount: _categories.length,
            padding: EdgeInsets.all(20.px),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              childAspectRatio: 1.5,
              crossAxisSpacing: 20.px,
              mainAxisSpacing: 20.px,
            ),
            itemBuilder: (ctx, index) {
              return JKHomeCategoryItem(_categories[index]);
            }
        );
      },
    );
  }
}
*/

class JKHomeContent extends StatefulWidget {

  @override
  _JKHomeContentState createState() => _JKHomeContentState();
}

class _JKHomeContentState extends State<JKHomeContent> {

  // 分类的model
  List<JKCategoryModel> _categories = [];

  @override
  void initState() {
    super.initState();

    JKRequestModel.getFoodCategory().then((result) {
      setState(() {
        _categories = result;
      });
    });

    /*
    JKJsonParse.getCategoryData().then((res) {
      setState(() {
        _categories = res;
      });
    });
    */
  }
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      itemCount: _categories.length,
      padding: EdgeInsets.all(20.px),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 1.5,
        crossAxisSpacing: 20.px,
        mainAxisSpacing: 20.px,
      ),
      itemBuilder: (ctx, index) {
        return JKHomeCategoryItem(_categories[index]);
      }
    );
  }
}

